#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
RESULTS_PATH <- file.path(R_ROOT, "Resultats")


#'#
#'# Chargement des dependances
#'# 

#source(file.path(R_WORKING_DIRECTORY, "Utils/initCluster.R"))
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))

#'#
#'# Donnees brutes et filtre LC
#'# 


MOBILE_WORKING_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")

mobileErrors <- loadErrorsFile(file.path(R_DATA_PATH, "Bylot", "mobile_errors.txt"))

mobileDataRaw <- loadData(MOBILE_WORKING_DIR, static=FALSE, raw=TRUE)
mobileDataGps <- do.call(rbind, lapply(mobileDataRaw, function(x) {return(x@gps)}))
mobileDataGps[, deployment := as.factor(deployment)]




densities <- getErrorDensities(mobileErrors)
generatedErrors <- lapply(densities, generateErrors, 1000)


#'#
#'# Select desired LC, number of points by track and weight of each LC
#'# 
locsClasses <- c("3", "2")

filteredErrors <- mobileErrors[LC %in% locsClasses]

nPoints <- filteredErrors[, length(argLong), by=deployment]$V1
locsWeights <- filteredErrors[, length(argLong)/length(filteredErrors$argLong), by=LC]$V1


#' Generate random points and random Errors
genPointsRadius1 <- generatePointsInRadius(1000L, 5000L)
genPointsRadius2 <- generatePointsInRadius(1000L, 250L)
genPointsRect <- generatePointsInRect(1000L, 8000L)
genPointsRect2 <- generatePointsInRect(1000L, 250L, 1000L)


#' Data generated from gps tracks
dataFromTracks <- generateHRCompareDataFromTracks(mobileDataGps, generatedErrors,
                                                  npoints=nPoints,
                                                  locsClasses=locsClasses,
                                                  locsWeights=locsWeights)
names(dataFromTracks) <- levels(mobileDataGps$deployment)



dataRadius <- generateHRCompareData(genPointsRadius1, generatedErrors,
                                     npoints=nPoints,
                                     locsClasses=locsClasses,
                                     locsWeight=locsWeights)

dataRadius2 <- generateHRCompareData(genPointsRadius2, generatedErrors,
                                     npoints=nPoints,
                                     locsClasses=locsClasses,
                                     locsWeight=locsWeights)
                                 
dataRect <- generateHRCompareData(genPointsRect, generatedErrors,
                                  npoints=nPoints,
                                  locsClasses=locsClasses,
                                  locsWeight=locsWeights)
                                 
dataRect2 <- generateHRCompareData(genPointsRect2, generatedErrors,
                                   npoints=nPoints,
                                   locsClasses=locsClasses,
                                   locsWeight=locsWeights)

                     
resRadius <- compareHRMobile(dataRadius, writeResults=FALSE, iterations=2L, kernelh=1000L, kernelPercent=95L)
resRadius2 <- compareHRMobile(dataRadius2, writeResults=FALSE, iterations=2L, kernelh=1000L, kernelPercent=95L)     
resRect <- compareHRMobile(dataRect, writeResults=FALSE, iterations=2L, kernelh=1000L, kernelPercent=95L)
resRect2 <- compareHRMobile(dataRect2, writeResults=FALSE, iterations=2L, kernelh=1000L, kernelPercent=95L)     

resTracks <- compareHRMobile(dataFromTracks, writeResults=FALSE, iterations=2L, kernelh=800L, kernelPercent=95L)     


statsCols <- c("sizeRatio", "overlapIndex", "propInRef")
stats <- resTracks[, c(lapply(.SD, mean), lapply(.SD, sd)), by=list(method, percent),
                   .SDcols=statsCols]
setnames(stats, 3:8, c(paste(statsCols, "mean", sep='.'), paste(statsCols, "sd", sep='.')))

